package se.webbzon.oschi01.utils;

import java.util.Collection;
import java.util.Map;
import java.util.Observable;
import java.util.Set;

public class ObservableMap<K,V> extends Observable implements Map<K,V> {
	
	private final Map<K,V> map;
	
	public ObservableMap(Map<K,V> map) {
		this.map = map;
	}

	@Override public void clear() {
		map.clear();
		setChanged();
		notifyObservers();
	}

	@Override public boolean containsKey(Object key) {
		return map.containsKey(key);
	}

	@Override public boolean containsValue(Object value) {
		return map.containsValue(value);
	}

	@Override public Set<java.util.Map.Entry<K, V>> entrySet() {
		return map.entrySet();
	}

	@Override public V get(Object key) {
		return map.get(key);
	}

	@Override public boolean isEmpty() {
		return map.isEmpty();
	}

	@Override public Set<K> keySet() {
		return map.keySet();
	}

	@Override public V put(K key, V value) {
		V oldValue = map.put(key, value);
		setChanged();
		notifyObservers();
		return oldValue;
	}

	@Override public void putAll(Map<? extends K, ? extends V> m) {
		map.putAll(m);
		setChanged();
		notifyObservers();
	}

	@Override public V remove(Object key) {
		V value = map.remove(key);
		if (value == null)
			return null;
		else {
			setChanged();
			notifyObservers();
			return value;
		}
	}

	@Override public int size() {
		return map.size();
	}

	@Override public Collection<V> values() {
		return map.values();
	}

}
